<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 用户表格
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-input v-model="searchName" placeholder="用户名" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="getData(1)">搜索</el-button>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
            >
                <el-table-column type="index" :index="indexMethod" label="#"></el-table-column>
                <el-table-column prop="id" label="用户编号"></el-table-column>
                <el-table-column prop="username" label="用户名"></el-table-column>
                <el-table-column prop="password" label="密码"></el-table-column>

                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                        <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="handleDelete(scope.$index, scope.row)"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :page-size="pageSize"
                    :total="pageTotal"
                    @current-change="getData"
                ></el-pagination>
            </div>
        </div>
        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="用户名">
                    <el-input v-model="form.username"></el-input>
                </el-form-item>
                <el-form-item label="密码">
                    <el-input v-model="form.password"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>

export default {
    name: 'basetable',
    data() {
        return {
            searchName: '',
            tableData: [
                {
                    id: 1,
                    username: '测试01',
                    password: '123',
                    money: '100.00'
                },{
                    id: 2,
                    username: '测试02',
                    password: '123',
                    money: '100.00'
                },{
                    id: 3,
                    username: '测试03',
                    password: '123',
                    money: '100.00'
                }
            ],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            pageTotal: 3,
            currentPage: 1,
            pageSize: 10,
            form: {},
            idx: -1,
            id: -1,

        };
    },
    created() {
        this.getData(1);
    },
    methods: {
        indexMethod(index) {
            const _index = index+1+(this.currentPage-1)*this.pageSize;
            return _index;
        },
        // 获取后台的数据
        getData(cpage) {
            const _this = this;
            this.currentPage = cpage;
            this.$http.get(`/userPage/${cpage}/${this.pageSize}?searchName=${this.searchName}`).then(function (resp) {
                _this.pageTotal = resp.data.total;
                _this.tableData = resp.data.list;
            });
        },
        // 删除操作
        handleDelete(index, row) {
            const _this = this;
            // 二次确认删除
            this.$confirm('确定要删除用户:  '+row.username, '提示', {
                type: 'warning'
            })
                .then(() => {
                    this.$http.delete('/deleteUser/'+row.id).then(function (resp) {
                        if (resp.data == 'success'){
                            _this.$message.success('删除成功');
                            _this.tableData.splice(index, 1);
                        }
                    })
                })
                .catch(() => {});
        },
        // 编辑操作
        handleEdit(index, row) {
            this.idx = index;
            const _this = this;
            this.$http.get('/search/'+row.id).then(function (resp) {
                _this.form = resp.data;
            });
            this.editVisible = true;
        },
        // 保存编辑
        saveEdit() {
            const _this = this;
            this.$http.post('/update',this.form).then(function (resp) {
                if (resp.data == 'success'){
                    _this.$message.success(`修改第 ${_this.idx + 1} 行成功`);
                    _this.getData(1);
                } else {
                    _this.$message.error(`修改第 ${_this.idx + 1} 行失败`);
                }
            });
            this.editVisible = false;
        },
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
